package stu.day1_day8;

public class LeetCode289 {
    public void gameOfLife(int[][] board) {
        int m = board.length;
        int n = board[0].length;
        int[][] newBoard = new int[m][n];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                newBoard[i][j] = seet(board, i, j);
            }
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                board[i][j] = newBoard[i][j];
            }
        }
    }

    public int seet(int[][] board,int i,int j) {
        int num = 0;

        for (int k = i-1; k < i+2; k++) {
            for (int l = j-1; l < j+2; l++) {
                if(k>=0&&k<board.length&&l>=0&&l<board[0].length) {
                    if(k==i&&l==j) {
                        continue;
                    }else {
                        if (board[k][l] == 1) {
                            num += 1;
                        }
                    }
                }
            }
        }
        if(board[i][j]==1&&(num<2||num>=4)) {
            return 0;
        } else if (board[i][j]==1&&(num==2||num==3)){
            return 1;
        }else if(board[i][j]==0&&num==3){
            return 1;
        }else{
            return 0;
        }

    }

    public static void main(String[] args) {

    }
}
